import Router from '../../../utils/router';
import Record from '../../../services/record-stat';
import base from '../../../config/base.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsList: {
      type: Object,
      value: {}
    },
    user_id: {
      type: String
    },
    store_id: {
      type: String
    },
    pagePosition: {
      type: String
    },
    pageName: {
      type: String
    },
    scrolltopnum: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        this.queryMultipleNodes(newVal)
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    icon_play: base.static + 'store/play2.png',
    icon_mask_sellout: base.static + 'store/csellout.png',
    icon_overseas: base.static + 'detail/haigou.png',
    goodsList: '',
    store_id: '',
    user_id: '',
    pagePosition: '',
    pageName: '',
  },
  ready() {
    let that = this
    let properties = that.properties;
    that.setData({
      goodsList: properties.goodsList,
      store_id: properties.store_id,
      user_id: properties.user_id,
      pagePosition: properties.pagePosition,
      pageName: properties.pageName,
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //去商品详情
    goDetail: function (e) {
      let that = this;
      let goods_id = e.currentTarget.dataset.goodsid;
      let store_id = e.currentTarget.dataset.storeid;
      Router.go('detail', `store_id=${store_id}&goods_id=${goods_id}`, 'navigateTo');
      that.viewRecord(goods_id, "click");
    },
    queryMultipleNodes(scroll) {
      let that = this;
      if (that.data.goodsList ==""){
        return
      }
      let query = wx.createSelectorQuery().in(that);
      let scrolls = 0;
      let index = 0;
      let s = wx.getSystemInfoSync().windowHeight;
      query.selectAll('.test').boundingClientRect(function (rects) {
        if (scrolls < scroll) {
          for (let i = index, len = rects.length; i < len; i++) {
            let rect = rects[i];
            try{
              if (rect && rect.top && 0 < rect.top && rect.bottom < s) {
                that.viewRecord(rect.id, "view")
                index = i;
                scrolls = scroll
              }
            }catch(e){}
          }
        } else {
          for (let i = index; i >= 0; i--) {
            let rect = rects[i];
            try{
              if (rect && rect.top && 0 < rect.top && rect.bottom < s) {
                index = i;
                scrolls = scroll
              }
            }catch(e){}
          }
        }
      }).exec()
    },
    //滚动和点击屏幕记录
    viewRecord: function (goodsid, act) {
      let that = this;
      let params = {
        name: "ltxx_buyer",
        page: that.data.pageName,
        position: that.data.pagePosition,
        productid: goodsid,
        userid: that.data.user_id,
        storeid: that.data.store_id,
        act: act,
        env: getApp().globalData.env
      }
      Record.recordstat(params, function () { });
    },
  }
})
